<?php
namespace Home\Controller;
use Common\Controller\CommonController;

class ProductController extends CommonController {

	/* *
	 * Dragon
	 * 2016-11-23
	 * 首页
	 * */
	function index() {
		$product = M('product');

		$Count=$product->count();
		$Page = new \Think\Pager($Count,10);
		$show = $Page->showhtml();
		$keyword=I('key');
		if(!empty($keyword)){
			$where['title'] = array('like',"%".$keyword."%");
		}

		$res = M('Product')->where($where)->order('sort desc,id asc')->limit($Page->firstRow.','.$Page->listRows)->select();

		$list = array();
		foreach ($res as $key => $value) {
			$value['listimg'] = json_decode($value['listimg'], true);
			$value['showimg'] = json_decode($value['showimg'], true);
			$value['listimg'] = $value['listimg'][0];
			//P($value);
			$numlist=M('Product_attr')->where(array('pid'=>$value['id']))->select();
			$connum=0;
			foreach ($numlist as $k => $v) {
				$connum+=intval($v['num']);
			}
			$value['num'] = $connum;
			$list[] = $value;
			
		}
		//P($list);
		$this->assign('page',$show);
		$this->assign('list', $list);
		$this->display();
	}
	/* *
	 * Dragon
	 * 2016-11-23
	 * 修改页
	 * */
	function product_uplist() {
		$where['id'] = I('id');
		$res = M('Product') -> where($where) -> find();
		$res['listimg'] = json_decode($res['listimg'], true);
		$res['showimg'] = json_decode($res['showimg'], true);
		//$res['ims'] = array(' /Public/upload/image/161123_112901_1307.png');
		//P($res['showimg']);
		$list = M('Product_category') -> select();
		$this->assign('list', $list);
		$this->assign('res', $res);
		$this->display('up_product');
	}

	/* *
	 * Dragon
	 * 2016-11-23
	 * 添加商品页
	 * */
	function product_list() {
		
		$res = M('Product_category') -> select();
		$this->assign('res', $res);
		$this->display('add_product');
	}

	public function reply()
	{
		$id = I('id');

        $wd = I('wd');
        $map = ['pid'=>$id];
        list($list, $page) = $this->lists('ProductReply', $map, 'id desc');
        if($list){
            if($user_ids = array_column($list, 'uid')){
                $user_arr = D('user')->where(['id'=>['in', $user_ids]])->getField('id,nickname');
            }else{
                $user_arr = [];
            }
            foreach ($list as $key => &$row) {
                if(isset($user_arr[$row['uid']])){
                    $row['nickname'] = $user_arr[$row['uid']];
                }else{
                    $row['nickname'] = '';
                }
                $row['img'] = json_decode($row['img']);
                if($row['img']){
					$imgdata = explode(',', $row['img']);
					if($imgdata){
						$row['img'] = $imgdata;
					}else{
                		$row['img'] = [];
					}
                }else{
                	$row['img'] = [];
                }
                $row['content'] = base64_decode($row['content']);
            }
        }
        $this->assign('page',$page);
        $this->assign('list', $list);
        $this->display();
	}

	public function reply_del()
	{
		$id = I('id');
        $res = M('ProductReply')->where(['id'=>$id])->delete();
        if($res){
            $this->ajaxReturn(array('s'=>true,'m'=>'删除成功', 'url'=>U('reply')));
        }else{
            $this->ajaxReturn(array('s'=>false,'m'=>'删除失败!'));
        }
	}

	/* *
	 * Dragon
	 * 2016-11-23
	 * 属性页
	 * */
	function product_attr() {
		$id = I('id');
		$where['pid'] = $id;
		$res = M('Product_attr') ->where($where) -> select();
		$this->assign('id', $id);
		$this->assign('res', $res);
		$this->display();
	}

	/* *
	 * Dragon
	 * 2016-11-23
	 * 添加产品
	 * */
	function add_product() {
		$title = I('title');
		$ds = I('ds');
		$cid = I('cid');
		$price = I('price');
		$list_src = I('list_src');
		$show_src = I('show_src');
		$content = I('content');
		$sort = I('sort');
		$data = array(
				'title' 	=> $title,
				'ds' 		=> $ds,
				'price' 	=> $price,
				'listimg' 	=> json_encode($list_src),
				'showimg' 	=> json_encode($show_src),
				'content' 	=> html_entity_decode($content),
				'ctime' 	=> time(),
				'cid'		=> $cid,
				'sort'		=> $sort,
			);

		$res = M('Product') -> add($data);
		if ($res) {
			$this->ajaxReturn(array('s' => true, 'm' => '添加成功'));
		} else {
			$this->ajaxReturn(array('s' => false, 'm' => '添加失败'));
		}

	}

	/* *
	 * Dragon
	 * 2016-11-23
	 * 修改产品
	 * */
	function up_product() {
		$title = I('title');
		$ds = I('ds');
		$cid = I('cid');
		$price = I('price');
		$list_src = I('list_src');
		$show_src = I('show_src');
		$content = I('content');
		$sort = I('sort');
		$data = array(
				'title' 	=> $title,
				'ds' 		=> $ds,
				'price' 	=> $price,
				'listimg' 	=> json_encode($list_src),
				'showimg' 	=> json_encode($show_src),
				'content' 	=> html_entity_decode($content),
				'cid'		=> $cid,
				'sort'		=> $sort,
			);

		$where['id'] = I('id');
		$res = M('Product') -> where($where) -> save($data);

		if ($res!==false) {
			$this->ajaxReturn(array('s' => true, 'm' => '修改成功'));
		} else {
			$this->ajaxReturn(array('s' => false, 'm' => '修改失败'));
		}

	}

	/* *
	 * Dragon
	 * 2016-11-23
	 * 上架、下架
	 * */
	function up_product_status() {
		$where['id'] = I('id');
		$data = array('status' => I('status'), );
		$res = M('Product') -> where($where) -> save($data);
		if ($res) {
			$this->redirect('Product/index');
		}
	}

	/* *
	 * Dragon
	 * 2016-11-23
	 * 删除产品
	 * */
	function del_product() {
		$where['id'] = I('id');
		$re = M('Product_attr') -> where(array('pid'=>I('id'))) -> delete();
		$res = M('Product') -> where($where) -> delete();
		if ($res) {
			$this->redirect('Product/index');
		}
	}
	
	/* *
	 * Dragon
	 * 2016-11-23
	 * 添加产品属性
	 * */
	function add_product_attr() {
		$pid=I('id');
		$name=I('name');
		$num=I('num');
		$dif=I('dif');
		$data = array('pid' => $pid, 'name' => $name, 'dif' => $dif,"num"=>$num );
		$res = M('Product_attr') ->add($data);
		if ($res) {
			$this->redirect('Product/product_attr',array('id'=>$pid));
		}
	}

	public function update_product_attr()
	{
        $id = I('id');
        $info = M('Product_attr')->where(['id'=>$id])->find();
        if(IS_POST){
            $data = I('post.');
            if(empty($data['name'])){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入名称'));
            }

            $model = M('Product_attr');
            $res = $model->where(['id'=>$id])->save($data);
            if($res!==false){
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('product_attr', ['id'=>$info['pid']])));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        }else{
            $this->assign('info', $info);
            $this->display();
        }
	}
	/* *
	 * Dragon
	 * 2016-11-23
	 * 删除产品属性
	 * */
	function del_product_attr() {
		$where['id'] = I('id');
		$pid = I('pid');
		$res = M('Product_attr') -> where($where) -> delete();
		if ($res) {
			$this->redirect('Product/product_attr',array('id'=>$pid));
		}
	}
	
	
	/* *
	 * Dragon
	 * 2017-02-09
	 * 轮播图
	 * */
	function shopindex() {
			
		$art=M('shop_img');
		$Count=$art->count();
		$Page = new \Think\Pager($Count,10);

		$show=$Page->showhtml();

		$res = M('shop_img') -> select();
		$list = array();
		foreach ($res as $key => $value) {

			$value['img'] = json_decode($value['img'], true);
			$value['img'] = $value['img'][0];
			$value['goods_name'] = M('product')->where(['id'=>$value['goods_id']])->getField('title');
			$list[] = $value;
		}
		$this->assign('page',$show);
		$this->assign('list', $list);
		$this->display();
	}
	
	public function slider_edit()
	{
		$id = I('id');
        if(IS_POST){
            $data = I('post.');
            $goods_name = I('goods_name');
            if(empty($goods_name)){
                $this->ajaxReturn(array('s'=>false,'m'=>'请输入商品名称!'));
            }
            $goods = M('product')->where(['title'=>$goods_name])->find();
            if(empty($goods_name)){
            	$this->ajaxReturn(array('s'=>false,'m'=>'商品不存在!'));
            }else{
            	$data['goods_id'] = $goods['id'];
            }
            $data['img'] = isset($data['img']) ? json_encode([$data['img']]) : '';

            $model = M('shop_img');
            $res = $model->where(['id'=>$id])->save($data);
            if($res!==false){
                $this->ajaxReturn(array('s'=>true,'m'=>'修改成功', 'url'=>U('shopindex')));
            }else{
                $this->ajaxReturn(array('s'=>false,'m'=>'修改失败!'));
            }
        }else{
            $info = M('shop_img')->where(['id'=>$id])->find();
            if($info){
            	$info['goods_name'] = M('product')->where(['id'=>$info['goods_id']])->getField('title');
            }
            $this->assign('info', $info);
            $this->display();
        }		
	}

	/* *
	 * Dragon
	 * 2016-11-25
	 * 添加轮播图
	 * */
	function add_advertising() {
		$title = I('title');
		$url = I("src");
		$img = json_encode(I("iocn"));
		$data = array( 'url' => $url, 'img' => $img, );

        $goods_name = I('goods_name');
        if(empty($goods_name)){
            $this->ajaxReturn(array('s'=>false,'m'=>'请输入商品名称!'));
        }
        $goods = M('product')->where(['title'=>$goods_name])->find();
        if(empty($goods)){
        	$this->ajaxReturn(array('s'=>false,'m'=>'商品不存在!'));
        }else{
        	$data['goods_id'] = $goods['id'];
        }

		$res = M('shop_img') -> add($data);
		if ($res) {
			$this->ajaxReturn(array('s' => true, 'm' => '添加成功'));
		} else {
			$this->ajaxReturn(array('s' => false, 'm' => '添加失败'));
		}
	}
	
		/* *
	 * Dragon
	 * 2016-11-25
	 * 删除轮播图
	 * */
	function del_advertising() {
		$where['id'] = I("id");
		$res = M('shop_img') -> where($where) -> delete();
		if ($res) {
			$this->redirect('Product/shopindex');
		}
	
	}
	
	/* *
	 * Dragon
	 * 2016-11-25
	 * 文章底部广告首页
	 * */
	function advertising() {
		
		$art=M('Product_advertising');
		$Count=$art->count();
		$Page = new \Think\Pager($Count,10);

		$show=$Page->showhtml();

		$res = $art -> select();
		$list = array();
		foreach ($res as $key => $value) {
			$value['img'] = json_decode($value['img'], true);
			$value['img'] = $value['img'][0];
			$list[] = $value;
		}
		$this->assign('page',$show);
		$this->assign('list', $list);
		$this->display();

	}

	/* *
	 * Dragon
	 * 2016-11-25
	 * 添加广告
	 * */
	function add_ad(){
		$title = I('title');
		$url = I("src");
		$img = json_encode(I("iocn"));
		$data = array('title' => $title, 'url' => $url, 'img' => $img, );
		$res = M('Product_advertising') -> add($data);
		if ($res) {
			$this->ajaxReturn(array('s' => true, 'm' => '添加成功'));
		} else {
			$this->ajaxReturn(array('s' => false, 'm' => '添加失败'));
		}
	}
	
	/* *
	 * Dragon
	 * 2016-11-25
	 * 删除广告
	 * */
	function del_ad() {
		$where['id'] = I("id");
		$res = M('Product_advertising') -> where($where) -> delete();
		if ($res) {
			$this->redirect('Product/advertising');
		}
	
	}
	/* *
	 * Dragon
	 * 2017-02-15
	 * 分类
	 * */
	function category() {
		$Count=M('Product_category')->count();
		$Page = new \Think\Pager($Count,10);
		$show = $Page->showhtml();
		$res = M('Product_category') ->limit($Page->firstRow.','.$Page->listRows)->select();
		$list = array();
		foreach ($res as $key => $value) {
			$value['cate_img'] = json_decode($value['cate_img'], true);
			$value['cate_img']=$value['cate_img'][0];
			$list[] = $value;
		}
		$this->assign('page',$show);
		$this->assign('list', $list);
		$this->display();
	
	}
	
		//添加板块
	 function add_category() {
	 	$category=I('category');
	 	$cate_img=json_encode(I('cate_img'));
	 	$data=array('category'=>$category,'cate_img'=>$cate_img);
	 	
	 	$res=M('Product_category')->add($data);
	 	if ($res) {
	 		$this->ajaxReturn(array('s' => true, 'm' => '添加成功'));
	 	} else {
	 		$this->ajaxReturn(array('s' => false, 'm' => '添加失败'));
	 	}		
	 }
	
	 function up_category() {
	 	$id=I('id');
		$list=M('Product_category')->where(array('id'=>$id))->find();
		$list['cate_img']=json_decode($list['cate_img'],true);
	 	if ($list) {
	 		$this->ajaxReturn(array('s' => true, 'd' => $list));
	 	} else {
	 		$this->ajaxReturn(array('s' => false));
	 	}		
	 }
	 
	 
	  function upp_category() {
	 	$id=I('id');
		$category=I('category');
	 	$cate_img=json_encode(I('cate_img'));
	 	$data=array('category'=>$category,'cate_img'=>$cate_img);
	 	$where['id']=$id;
	 	$res=M('Product_category')->where($where)->save($data);
	 	if ($res) {
	 		$this->ajaxReturn(array('s' => true, 'm' => '修改成功'));
	 	} else {
	 		$this->ajaxReturn(array('s' => false, 'm' => '修改失败'));
	 	}		
	 }
	 function del_category() {
	 	
	 	$where['id'] = I('id');
	 	$res = M('Product_category') -> where($where) -> delete();
	 	if ($res) {
	 		$this->ajaxReturn(array('s' => true, ));
	 	} else {
	 		$this->ajaxReturn(array('s' => false, ));
	 	}	
	 }
	
}
